package com.atgm.mall.gmmailmember.service.impl;

import com.atgm.common.exception.BizCodeEnume;
import com.atgm.common.exception.MemberException;
import com.atgm.common.utils.GmMallUtils;
import com.atgm.common.utils.R;
import com.atgm.mall.gmmailmember.dao.MemberThumbsReplyDao;
import com.atgm.mall.gmmailmember.entity.MemberThumbsReplyEntity;
import com.atgm.mall.gmmailmember.feign.ClubFeignService;
import com.atgm.mall.gmmailmember.service.MemberThumbsReplyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MemberThumbsReplyServiceImpl extends ServiceImpl<MemberThumbsReplyDao, MemberThumbsReplyEntity> implements MemberThumbsReplyService {

    @Autowired
    private ClubFeignService clubFeignService;

    @Override
    public Integer saveThumbsReplyInfo(MemberThumbsReplyEntity memberThumbsReplyEntity) {
        //检查该用户是否点赞该回复
        QueryWrapper<MemberThumbsReplyEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        queryWrapper.eq("reply_id", memberThumbsReplyEntity.getReplyId());
        queryWrapper.eq("member_id", memberThumbsReplyEntity.getMemberId());
        MemberThumbsReplyEntity selectOne = baseMapper.selectOne(queryWrapper);
        int flag = 0;
        if (selectOne == null) {  //没有点赞
            memberThumbsReplyEntity.setDeleted(0);
            memberThumbsReplyEntity.setCreateTime(new Date());
            int count = baseMapper.insert(memberThumbsReplyEntity);
            if (count != 1) {
                throw new MemberException("点赞失败", BizCodeEnume.MEMBER_THUMBS.getCode());
            }
            flag = 1;
        }else {
            int count = baseMapper.deleteById(selectOne.getId());
            if (count != 1) {
                throw new MemberException("点赞失败", BizCodeEnume.MEMBER_THUMBS.getCode());
            }
        }

        R r = clubFeignService.updateReplyThumbsCount(memberThumbsReplyEntity.getReplyId(), flag);
        if (0 != r.getCode()) {
            throw new MemberException("点赞失败", BizCodeEnume.MEMBER_THUMBS.getCode());
        }

        return flag;
    }

    @Override
    public List<Long> queryThumbsReplyIds(Long memberId, Long articleId) {
        QueryWrapper<MemberThumbsReplyEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("reply_id");
        queryWrapper.eq("member_id", memberId);
        queryWrapper.eq("article_id", articleId);
        List<MemberThumbsReplyEntity> list = baseMapper.selectList(queryWrapper);

        List<Long> outList = new ArrayList<>();
        if (GmMallUtils.isNotEmpty(list)) {
            outList = list.stream().map(MemberThumbsReplyEntity::getReplyId).collect(Collectors.toList());
        }

        return outList;
    }
}
